package com.boyuan.huijiaoyu;

import com.boyuan.huijiaoyu.R;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

public class SettingPolicyActivity extends BaseActivity implements
		OnClickListener {

	private final String TAG = "SettingPolicyActivity";

	private TextView txtTitle;
	private Button btnBack;
	private WebView webView;

	private String website;
	private String title;
	private int isShowLoading = 0;//1为商品列表 不需要loading 0位默认需要loading

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.policy);

		Intent data = getIntent();
		if (data.getExtras() != null) {
			website = data.getExtras().getString("addr");
			title = data.getExtras().getString("title");
			isShowLoading = data.getExtras().getInt("isloading");

		} else {
			website = "http://www.colourlife.com/help?category_id=30";
			title = getResources().getString(R.string.policy_title);// 用户协议
		}
		
		prepareView();
	}

	private void prepareView() {

		txtTitle = (TextView) findViewById(R.id.txt_title);
		txtTitle.setText(title);

		btnBack = (Button) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				onBackPressed();
			}
		});

		webView = (WebView) findViewById(R.id.webview);
		webView.requestFocusFromTouch();

		webView.loadUrl(website);

		webView.setWebViewClient(new WebViewClient() {

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {

				// 数据加载中……
				if (isShowLoading == 0) {
					showLoading(getResources().getString(R.string.loading_data));
				}

				super.onPageStarted(view, url, favicon);
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				hideLoading();
				super.onPageFinished(view, url);
			}

		});
		WebSettings settings = webView.getSettings();
		settings.setJavaScriptEnabled(true);
		settings.setLoadsImagesAutomatically(true);
		settings.setUseWideViewPort(true);
		settings.setBuiltInZoomControls(true);
		settings.setLoadWithOverviewMode(true);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
	}

	@Override
	public void backClicked() {

		if (isShowingLoading()) {

			Log.e(TAG, "hideLoading");

			hideLoading();

		} else {
			isBackAllowed = true;
		}
	}
}
